<?php

namespace app\admin\controller;

use app\BaseController;
use app\service\admin\AdminLogic;
use think\facade\Session;

class Login extends BaseController
{
    public function index()
    {
        if (request()->isPost()) {
            $username = request()->param('username');
            $password = request()->param('password');
            $arrAdmin = AdminLogic::getInstance()->check($username, md5($password));
            if (empty($arrAdmin)) {
                return error('账号或密码错误');
            }
            Session::set('admin_id', $arrAdmin['id']);
            Session::set('real_name', $arrAdmin['real_name']);
            Session::set('role_id', $arrAdmin['role_id']);
            return success();
        } else {
            if (!empty(Session::get('admin_id'))) {
                return redirect('/admin/index/index');
            }
            return view();
        }
    }

    public function logout()
    {
        Session::clear();
        return  redirect('/admin/login/index');
    }
}